package com.tjsinfo.tjsoa.system.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.articles.bo.ManageSuperAction;
import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.system.bo.ITSystemBeiWangBo;
import com.tjsinfo.tjsoa.system.vo.TSystemBeiWang;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class TSystemBeiWangAction extends ManageSuperAction{

	private ITSystemBeiWangBo tSystemBeiWangBo;
	
	public ITSystemBeiWangBo gettSystemBeiWangBo() {
		return tSystemBeiWangBo;
	}

	public void settSystemBeiWangBo(ITSystemBeiWangBo tSystemBeiWangBo) {
		this.tSystemBeiWangBo = tSystemBeiWangBo;
	}
    //新增备忘
	public ActionForward addBeiWang(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
//		TSystemUser sessionUser = (TSystemUser)request.getSession().getAttribute("articlesUser");
		TSystemUser systemUser = new TSystemUser();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		systemUser.setUserId(user.getUser_id());
        TSystemBeiWang systemBeiWang = new TSystemBeiWang();
        systemBeiWang.setTSystemUser(systemUser);
        String uuid = UUID.randomUUID().toString();
        String date = request.getParameter("datetime");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        systemBeiWang.setDatetime(format.parse(date));
        systemBeiWang.setMessage(request.getParameter("message"));
        systemBeiWang.setUuid(uuid);
        tSystemBeiWangBo.save(systemBeiWang);
		return mapping.findForward("success");
	}
    //查询备忘并分页
	public ActionForward queryBeiwang(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//从session中获得用户信息
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int id = user.getUser_id();
		String rq = request.getParameter("date");
		String start = request.getParameter("start");
		if(start==null) start="0";
		String limit = request.getParameter("limit");
		if(limit==null) limit="10";
		
		List<TSystemBeiWang> listPage = tSystemBeiWangBo.queryByUser(id, start, limit,rq);
		List<TSystemBeiWang> listAll = tSystemBeiWangBo.queryAll(id);
		JSONArray array = new JSONArray();
		try {
			for(TSystemBeiWang systemBeiWang:listPage){
				JSONObject object = new JSONObject(systemBeiWang);
				array.put(object);
			}
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("totalProperty", listAll.size());
			jsonObject.put("root", array);
			response.getWriter().print(jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//删除备忘
	public ActionForward deleteBeiwang(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String beiwang_uuid = request.getParameter("uuid");
		tSystemBeiWangBo.deleteBeiWang(beiwang_uuid);
		return mapping.findForward("deleteBeiwang");
	}
	//处理更新备忘数据
	public ActionForward toUpdateBeiwang(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		String uuid = request.getParameter("uuid");
		TSystemBeiWang systemBeiWang = new TSystemBeiWang();
		systemBeiWang = tSystemBeiWangBo.queryByUuid(uuid);
		String datetime = format.format(systemBeiWang.getDatetime());
		String message = systemBeiWang.getMessage();
		
		request.setAttribute("numb", "1");
		request.setAttribute("datetime", datetime);
		request.setAttribute("message", message);
		request.setAttribute("id", Integer.toString(systemBeiWang.getId()));
		return mapping.findForward("toupdatebeiwang");
	}
	//更新备忘
	public ActionForward updateBeiwang(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String datetime = request.getParameter("datetime");
		String message = request.getParameter("message");
		String id = request.getParameter("id");
		
		tSystemBeiWangBo.updateBeiwang(datetime, message, id);
		return mapping.findForward("updateok");
		
	}
	//按照USERID查询该人所有任务
	public ActionForward getTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Integer userId = user.getUser_id();
		List<TSystemBeiWang> list = tSystemBeiWangBo.queryAll(userId);
		JSONArray array = new JSONArray();
		for(TSystemBeiWang systemBeiWang:list){
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String date = dateFormat.format(systemBeiWang.getDatetime());
			date = DayFormat.getYMDDays(date);
			String beiwangTime = dateFormat2.format(systemBeiWang.getDatetime());
			String hour = beiwangTime.substring(beiwangTime.indexOf(" ")+1,beiwangTime.length());
			JSONObject object = new JSONObject();
			object.put("datetime", date);
			object.put("hour", hour);
			object.put("message", systemBeiWang.getMessage());
			object.put("id", systemBeiWang.getId());
			array.put(object);
		}
		response.getWriter().print(array);
		return null;
	}
}
